//
//  ComicViewController.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/8/26.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ComicViewController.h"
//#import "JHMacro.h"
#import "ComicTableViewCell.h"
#import "myView.h"
#import "LoginViewController.h"
#import "ComicTableViewCell.h"
#import "ZWYNetTool.h"
#import "ComicModel.h"
#import "TopicModel.h"
#import "UserModels.h"
#import "comicDataModel.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "ViewController.h"
@interface ComicViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)NSArray *SegmentArray;// 分块数组
@property(nonatomic,strong)UISegmentedControl *segment;// 分块
@property(nonatomic,strong)NSMutableArray *Array;// scrroll数组
@property(nonatomic,strong)UIScrollView *ComicScrrollView;// 大界面
@property(nonatomic,strong)UIScrollView *UpScrrollView;// 日期滑动视图
@property(nonatomic,strong)UIScrollView *hotScrrollView;// 热门滑动视图
@property(nonatomic,strong)UITableView *mytableview;
@property(nonatomic,strong)myView *myview;// 自定义view
@property(nonatomic,strong)comicDataModel *model;
@property(nonatomic,assign)NSInteger number;
@property(nonatomic,strong)NSMutableArray *tagArr;
//@property(nonatomic,strong)NSArray *timeArr;
@end

@implementation ComicViewController
- (void)viewWillAppear:(BOOL)animated {
    [self Follow];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tagArr = [NSMutableArray array];
    [self NetWork];
    // 设置导航栏不透明
    self.navigationController.navigationBar.translucent = NO;
    UIImage *image = [UIImage imageNamed:@"IMG_0427"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(seek)];
    self.navigationItem.rightBarButtonItem = item;
    self.Array = [NSMutableArray arrayWithObjects:@"周三",@"周四",@"周五",@"周六",@"周日",@"昨天",@"今天", nil];
    // 设置导航栏标题视图
    [self setUp];
    // 热门界面
    [self hot];
    // 关注界面
    [self Follow];
    
}
- (void)seek {
    ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
//    [self.navigationController pushViewController:vc animated:YES];
    [self presentViewController:vc animated:YES completion:nil];
}
// 封装UISegmentedControl
- (void)setUp {
    self.SegmentArray = [NSArray arrayWithObjects:@"关注",@"热门", nil];
    // 分块控件
    self.segment = [[UISegmentedControl alloc]initWithItems:_SegmentArray];
    // 布局
    self.segment.frame = CGRectMake(0, 0, 140, 30);
    // 倒圆角
    CALayer *layer = self.segment.layer;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:15];
    // 初始偏移量
    self.segment.selectedSegmentIndex = 1;
    // 控件颜色
    self.segment.tintColor = [UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000];
    // 控件方法
    [self.segment addTarget:self action:@selector(segmentAction) forControlEvents:(UIControlEventValueChanged)];
    // 设置为导航栏头视图
    self.navigationController.navigationBar.topItem.titleView = _segment;
}
// ******************* 网络请求
- (void)NetWork {
    for (int i = 0; i < 7; i ++) {
        if (self.myview.frame.origin.x / 375 == i) {
            [ZWYNetTool GET:@"http://api.kuaikanmanhua.com/v1/daily/comic_lists/0" andBody:@"since=0" andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
                NSLog(@"=====================成功啦");
                self.model = [comicDataModel modelWithDic:result[@"data"]];
                [self.mytableview reloadData];
            } andFailureBlock:^(NSError *error) {
                NSLog(@"=====================出错啦");
            }];
        }
    }
}
// ************************ 热门界面
- (void)hot {
    // 布局
    self.ComicScrrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    // 偏移量
    _ComicScrrollView.contentOffset = CGPointMake(self.view.bounds.size.width, 0);
    // 整页翻动
    _ComicScrrollView.pagingEnabled = YES;
    // 是否回弹
    _ComicScrrollView.bounces = NO;
    // 滚动范围
    _ComicScrrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
    // 添加到主视图
    [self.view addSubview:self.ComicScrrollView];
    _ComicScrrollView.delegate = self;
    // 热门界面滚动视图
    self.hotScrrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    // 偏移量
    _hotScrrollView.contentOffset = CGPointMake(self.view.bounds.size.width * 6, self.view.bounds.size.height);
    // 滚动范围
    _hotScrrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 7, self.view.bounds.size.height);
    // 整页滚动
    _hotScrrollView.pagingEnabled = YES;
    // 是否回弹
    _hotScrrollView.bounces = NO;
    _hotScrrollView.delegate = self;
    for (int i = 0; i < 7; i ++) {
        // 循环创建自定义视图
        self.myview = [[myView alloc]initWithFrame:CGRectMake(i * self.view.bounds.size.width, 30, self.view.bounds.size.width, self.view.bounds.size.height - 143)];
        [self.hotScrrollView addSubview:self.myview];
    }
    [self.ComicScrrollView addSubview:self.hotScrrollView];
    // 头视图
    self.UpScrrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, 30)];
    // 偏移量
    _UpScrrollView.contentOffset = CGPointMake(self.view.bounds.size.width / 5 * 6, 0);
    // 滚动范围
    _UpScrrollView.contentSize = CGSizeMake(self.view.bounds.size.width / 5 * 7, 30);
    _UpScrrollView.delegate = self;
    // 循环创建button
    for (int i = 0; i < 7; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * (self.view.bounds.size.width / 5), 0, self.view.bounds.size.width / 5, 30);
        [btn setTitle:[NSString stringWithFormat:@"%@",_Array[i]] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_tagArr addObject:btn];
        if (i == 6) {
            btn.selected = YES;
            self.SBtn = btn;
            [btn setTitleColor:[UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000] forState:(UIControlStateNormal)];
        }
        [_UpScrrollView addSubview:btn];
    }
    [self.ComicScrrollView addSubview:_UpScrrollView];
}

- (void)btnAction:(UIButton *)btn {
    if (btn.selected == YES) {
//        _hotScrrollView.contentOffset = CGPointMake(btn.frame.origin.x * 5, 0);
        [_hotScrrollView setContentOffset:CGPointMake(btn.frame.origin.x * 5, 0) animated:YES];
    } else {
        self.SBtn.selected = NO;
        [self.SBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        btn.selected = YES;
        self.SBtn = btn;
        if (btn.frame.origin.x != 0 && btn.frame.origin.x != btn.frame.size.width && btn.frame.origin.x != 5 * btn.frame.size.width && btn.frame.origin.x != 6 * btn.frame.size.width) {
//            _UpScrrollView.contentOffset = CGPointMake(btn.frame.origin.x - (2 * btn.frame.size.width), 0);
            [_UpScrrollView setContentOffset:CGPointMake(btn.frame.origin.x - (2 * btn.frame.size.width), 0) animated:YES];
        }
        [btn setTitleColor:[UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000] forState:(UIControlStateNormal)];
//        _hotScrrollView.contentOffset = CGPointMake(btn.frame.origin.x * 5, 0);
        [_hotScrrollView setContentOffset:CGPointMake(btn.frame.origin.x * 5, 0) animated:YES];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
        scrollView = _hotScrrollView;
        NSInteger number = scrollView.contentOffset.x / self.view.bounds.size.width;
        if (number != 0 && number != 1 && number != 5 && number != 6) {
            _UpScrrollView.contentOffset = CGPointMake(number * 75 - 150, 0);
        }
        for (UIButton *button in _tagArr) {
            [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        }
        UIButton *btn = _tagArr[number];
        [btn setTitleColor:[UIColor colorWithRed:0.953 green:0.651 blue:0.075 alpha:1.000] forState:(UIControlStateNormal)];
        if (_ComicScrrollView.contentOffset.x == 0) {
            _segment.selectedSegmentIndex = 0;
        } else if (_ComicScrrollView.contentOffset.x != 0) {
            _segment.selectedSegmentIndex = 1;
        }   
}
// ************************ 关注界面
- (void)Follow {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSInteger num = [user integerForKey:@"isLogin"];
    if (num == 0) {
        // 未登录界面布局
        UIView *followView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        // 背景颜色
        followView.backgroundColor = [UIColor colorWithWhite:0.940 alpha:1.000];
        // 图片控制器
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - 100, 150, 200, 150)];
        // 图片
        imgview.image = [UIImage imageNamed:@"5"];
        // 添加到followView
        [followView addSubview:imgview];
        // 登录按钮
        UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        // 按钮title
        [loginBtn setTitle:@"立即登录" forState:(UIControlStateNormal)];
        // 文字颜色
        [loginBtn setTitleColor:[UIColor colorWithRed:0.529 green:0.447 blue:0.173 alpha:1.000] forState:(UIControlStateNormal)];
        // 按钮布局
        loginBtn.frame = CGRectMake(self.view.bounds.size.width / 2 - 60, 370, 120, 50);
        // 倒圆角
        CALayer *layer = loginBtn.layer;
        [layer setMasksToBounds:YES];
        [layer setCornerRadius:25];
        // 背景颜色
        loginBtn.backgroundColor = [UIColor colorWithRed:0.973 green:0.827 blue:0.314 alpha:1.000];
        // 按钮方法
        [loginBtn addTarget:self action:@selector(motal) forControlEvents:(UIControlEventTouchUpInside)];
        // 添加到followView
        [followView addSubview:loginBtn];
        [self.ComicScrrollView addSubview:followView];
    } else {
        // 登录后界面布局
        UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:(UITableViewStylePlain)];
        // 设置代理人
        tableview.delegate = self;
        tableview.dataSource = self;
        [tableview registerNib:[UINib nibWithNibName:@"ComicTableViewCell" bundle:nil] forCellReuseIdentifier:@"comic"];
        [self.ComicScrrollView addSubview:tableview];
    }
}
- (void)motal {
    // 模态弹出登录界面
    LoginViewController *LoginVC = [[LoginViewController alloc]init];
    UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:LoginVC];
    [self presentViewController:nvc animated:YES completion:nil];
}
// 自定义cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 400;
}
// 每个分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.comics.count;
}
// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    [self NetWork];
    ComicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comic" forIndexPath:indexPath];
    ComicModel *comic = self.model.comics[indexPath.row];
    cell.typeLabel.text = comic.label_text;
    cell.titleLabel.text = comic.topic.title;
    cell.nameLabel.text = comic.topic.user.nickname;
    cell.numberLabel.text = comic.title;
    [cell.numberBtn setTitle:[NSString stringWithFormat:@"%@",comic.likes_count] forState:(UIControlStateNormal)];
    [cell.sayBtn setTitle:[NSString stringWithFormat:@"%@",comic.comments_count] forState:(UIControlStateNormal)];
    [cell.PhotoImageview sd_setImageWithURL:[NSURL URLWithString:comic.cover_image_url]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// 分块控件方法
- (void)segmentAction {
    if (self.segment.selectedSegmentIndex == 0) {
        // 控制滚动视图偏移量
        self.ComicScrrollView.contentOffset = CGPointMake(0, 0);
    } else if (self.segment.selectedSegmentIndex == 1) {
        self.ComicScrrollView.contentOffset = CGPointMake(self.view.bounds.size.width, 0);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
